In [1]:
%reload_ext autoreload
%autoreload 2
%matplotlib inline

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(context="notebook", style="darkgrid", palette="dark")

import sys
sys.path.append('..')

from helper import linear_regression as lr  # my own module
from helper import general as general

In [2]:
raw_data = pd.read_csv('ex1data2.txt', names=['square', 'bedrooms', 'price'])
raw_data.head()


Out[2]:
square bedrooms price
0 2104 3 399900
1 1600 3 329900
2 2400 3 369000
3 1416 2 232000
4 3000 4 539900

1. normalize data


In [3]:
data = general.normalize_feature(raw_data)
data.head()


Out[3]:
square bedrooms price
0 0.130010 -0.223675 0.475747
1 -0.504190 -0.223675 -0.084074
2 0.502476 -0.223675 0.228626
3 -0.735723 -1.537767 -0.867025
4 1.257476 1.090417 1.595389

2. multi-var batch gradient decent


In [4]:
X = general.get_X(data)
print(X.shape, type(X))

y = general.get_y(data)
print(y.shape, type(y))


(47, 3) <class 'numpy.ndarray'>
(47,) <class 'numpy.ndarray'>

In [5]:
alpha = 0.01
theta = np.zeros(X.shape[1])
epoch = 500

In [6]:
final_theta, cost_data = lr.batch_gradient_decent(theta, X, y, epoch, alpha=alpha)

In [11]:
sns.tsplot(time=np.arange(len(cost_data)), data = cost_data)


Out[11]:
<matplotlib.axes._subplots.AxesSubplot at 0x118cc1b00>

In [8]:
final_theta


Out[8]:
array([ -1.22797753e-16,   8.30383883e-01,   8.23982853e-04])

3. learning rate


In [9]:
base = np.logspace(-1, -5, num=4)
candidate = np.sort(np.concatenate((base, base*3)))
print(candidate)


[  1.00000000e-05   3.00000000e-05   2.15443469e-04   6.46330407e-04
   4.64158883e-03   1.39247665e-02   1.00000000e-01   3.00000000e-01]

In [10]:
epoch=50

fig, ax = plt.subplots(figsize=(16, 9))

for alpha in candidate:
    _, cost_data = lr.batch_gradient_decent(theta, X, y, epoch, alpha=alpha)
    ax.plot(np.arange(epoch+1), cost_data, label=alpha)

ax.set_xlabel('epoch', fontsize=18)
ax.set_ylabel('cost', fontsize=18)
ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
ax.set_title('learning rate', fontsize=18)


Out[10]:
<matplotlib.text.Text at 0x118a79390>

4. normal equation

this is fancy, but I don't see how this could help for non-convex optimization... so skip this one


In [ ]: